﻿//JumbotCms_4.1 Final
using System;
using JumbotCms.Common;

namespace JumbotCms.WebFile.Passport
{
    public partial class _register : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!site.AllowReg)
            {
                FinalMessage("对不起，本系统暂停注册!", site.Dir, 0);
                Response.End();
            }
            if (q("step") == "2")
            {
                if (Jumbot.Common.Session.Get("jcms_user_register") != "1")
                {
                    Response.Write("请勿随便试这个功能");
                    Response.End();
                }
                if (q("act") != "save")
                    Response.Write(GetPageHtml("passport_registerStep2"));
                else
                {
                    if (!CheckFormUrl())
                        Response.End();
                    string uName = f("txtUserName");
                    string uMail = f("txtEmail");
                    string mailsign = f("txtMailSign");
                    string _body = uName + ", 您好！<br>" +
                        "感谢您注册" + site.Name + "，点击下面的链接即可完成注册：<br>" +
                        "<a href=\"" + site.Url + site.Dir + "user/active/index.aspx?username=" + Server.UrlEncode(uName) + "&amp;email=" + uMail + "&amp;mailsign=" + mailsign + "\" target=\"_blank\">" +
                        site.Url + site.Dir + "user/active/index.aspx?username=" + Server.UrlEncode(uName) + "&amp;email=" + uMail + "&amp;mailsign=" + mailsign + "</a><br>" +
                        "（如果链接无法点击，请将它拷贝到浏览器的地址栏中）";
                    if (SendMail(uMail, site.Name + "注册激活邮件", _body))
                    {
                        Session["jcms_user_register"] = "";
                        Response.Write("JumbotCms.Alert('激活邮件已经发送', '1');");
                    }
                    else
                        Response.Write("JumbotCms.Alert('发送失败，请稍候再试', '0');");
                }
            }
            else
            {
                string _registerType = Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/user", "RegisterType");
                if (q("act") != "save")
                {
                    Response.Write(GetPageHtml("passport_registerStep1(" + _registerType + ")"));
                }
                else
                {
                    if (!CheckFormUrl())
                        Response.End();
                    string _code = f("txtCode");
                    if (Jumbot.Common.Cookie.GetValue("ValidateCode") != _code)
                    {
                        Response.Write("JumbotCms.Alert('验证码错误', '0');");
                        Response.End();
                    }
                    if (f("txtUserName").Length > 0 && f("txtPass1").Length > 0 && f("txtEmail").Length > 0)
                    {
                        int dPoints = Str2Int(Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/user", "DefaultPoints"), 0);
                        int uState = site.CheckReg ? 0 : 1;
                        string mailsign = GetRandomNumberString(64, false);
                        object[,] addFields = new object[2, 28] { 
                        {
                            "UserName", "UserPass", "TrueName", "Sex", "Email", "Birthday", "ComeFrom", 
                            "IDType", "IDCard", "WorkUnit", "ZipCode", "MobileTel", "Telephone", "QQ", "MSN", 
                            "Group", "Points", "Login", "State", "IsWriter", "AdminId", "Setting", "MailSign", 
                            "AdminState", "IsVIP", "Integral","RegTime", "RegIp"}, 
                        {
                            f("txtUserName"),GetHash32(f("txtPass1")), f("txtTrueName"), Str2Int(f("rblSex")), f("txtEmail"), f("txtBirthday"), f("txtComeFrom"), 
                            Str2Int(f("ddlIDType")), f("txtIDCard"), f("txtWorkUnit"), f("txtZipCode"), f("txtMobileTel"), f("txtTelephone"), f("txtQQ"), f("txtMSN"), 
                            1, dPoints, 0, uState,0, 0, ",,", mailsign, 
                            0,0, 0, DateTime.Now.ToString(),GetUserIp()} 
                        };
                        doh.Reset();
                        doh.AddFieldItems(addFields);
                        int _uID = doh.Insert("jcms_normal_user");
                        doh.Reset();
                        doh.ConditionExpress = "id=1";
                        doh.Add("jcms_normal_usergroup", "UserTotal");

                        if (site.ForumPath != "")
                        {
                            string _ForumAutoRegister = Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/api", "ForumAutoRegister");
                            if (_ForumAutoRegister == "user")//表示自动注册论坛用户
                            {
                                bool IsSuc = Jumbot.API.Discuz.Command.Register(f("txtUserName"), Str2Int(f("rblSex")), f("txtUserName"), f("txtPass1"), f("txtEmail"));
                                if (IsSuc)
                                {
                                    //注册成功
                                    doh.Reset();
                                    doh.ConditionExpress = "id=" + _uID;
                                    doh.AddFieldItem("ForumName", f("txtUserName"));
                                    doh.AddFieldItem("ForumPass", GetHash32(f("txtPass1")));
                                    doh.Update("jcms_normal_user");
                                }
                            }
                        }
                        if (uState == 0)//说明需要邮件激活
                        {
                            string _body = f("txtUserName") + ", 您好！<br>" +
                                "感谢您注册" + site.Name + "，点击下面的链接即可完成注册：<br>" +
                                "<a href=\"" + site.Url + site.Dir + "user/active/index.aspx?username=" + Server.UrlEncode(f("txtUserName")) + "&amp;email=" + f("txtEmail") + "&amp;mailsign=" + mailsign + "\" target=\"_blank\">" +
                                site.Url + site.Dir + "user/active/index.aspx?username=" + Server.UrlEncode(f("txtUserName")) + "&amp;email=" + f("txtEmail") + "&amp;mailsign=" + mailsign + "</a><br>" +
                                "（如果链接无法点击，请将它拷贝到浏览器的地址栏中）";
                            if (SendMail(f("txtEmail"), site.Name + "注册激活邮件", _body))
                            {
                                Session["jcms_user_register"] = "1";
                                Response.Write("location='" + site.Dir + "passport/register.aspx?step=2&username=" + Server.UrlEncode(f("txtUserName")) + "&amp;email=" + f("txtEmail") + "&amp;mailsign=" + mailsign + "';");
                            }
                            else
                                Response.Write("JumbotCms.Alert('注册成功，但由于某种原因，邮件发送失败', '1', \"window.location='" + site.LoginUrl + "';\");");
                        }
                        else
                        {
                            Response.Write("JumbotCms.Alert('注册成功，请登录', '1', \"window.location='" + site.LoginUrl + "';\");");
                        }
                    }
                    else
                        Response.Write("JumbotCms.Alert('提交有误', '0');");
                }
            }
        }

    }
}
